home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / utility / freedos.zip / COM050.ZIP / WHERE.C < prev   
C/C++ Source or Header  |  1996-01-17  |  4KB  |  160 lines

  1. /* 
  2.  *  WHERE.C - path functions.
  3.  *
  4.  *
  5.  *
  6.  *  Comments:
  7.  *
  8.  *  07/15/95 (Tim Norman) ---------------------------------------------------
  9.  *    started.
  10.  *
  11.  *  08/08/95 (Matt Rains) ---------------------------------------------------
  12.  *    i have cleaned up the source code. changes now bring this source into
  13.  *    guidelines for recommended programming practice.
  14.  *
  15.  *  12/12/95 (Steffan Kaiser & Tim Norman) ----------------------------------
  16.  *    added some patches to fix some things and make more efficient
  17.  *
  18.  *  1/6/96 (Tim Norman) -----------------------------------------------------
  19.  *    fixed a stupid pointer mistake...  Thanks to everyone who noticed it!
  20.  *
  21.  */
  22.  
  23. #include <dos.h>
  24. #include <dir.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. /*
  29.  *
  30.  *  get paths from env. var.
  31.  *
  32.  */
  33. void get_paths(char *paths[129])
  34. {
  35.    char *p;                     /* pointer to path */
  36.    char *tmp;
  37.    static char path[140];       /* path with .\ added to it */
  38.    int count, pos;              /* counters */
  39.  
  40.    /* getenv() returns a pointer to a string containing the environment of
  41.       the program. then add the current directory to the search path, put in 
  42.       new variable */
  43.  
  44.    p = getenv("PATH"); 
  45.    strcpy(path, ".;"); 
  46.    strcat(path, p);    
  47.  
  48.    /* set the first search path to point to the whole path (will add NULL 
  49.       terminator in right spot later */
  50.  
  51.    paths[0] = path; 
  52.    pos = 0;
  53.    count = 1;
  54.  
  55.    while(path[pos] != 0) /* step through chars in path */
  56.    {
  57.       if(path[pos] == ';')
  58.       {
  59.          path[pos] = 0; /* terminate it and inc. the path pointer */
  60.  
  61.          if(path[pos + 1])
  62.          {
  63.             paths[count] = &path[pos + 1];
  64.             count++;
  65.          }
  66.       }
  67.       pos++;
  68.    }
  69.  
  70.    paths[count] = 0; /* last path points to NULL */
  71.  
  72.    count = 0;
  73.  
  74.    while(paths[count]) /* strip backslash from end of paths */
  75.    {
  76.       tmp = &paths[count][strlen(paths[count]) - 1];
  77.  
  78.       if(*tmp == '\\')
  79.       {
  80.          *tmp = 0;
  81.       }
  82.  
  83.       count++;
  84.    }
  85.  
  86.    /* paths is now an array of pointers to all the search paths */
  87.  
  88.    return;
  89. }
  90.  
  91. /*
  92.  *
  93.  *  searches for file using path info.
  94.  *
  95.  */
  96. int find_which(char *paths[129], char *fname, char *fullname)
  97. {
  98.    struct ffblk f; /* directory search structure */
  99.  
  100.    int count;
  101.    int tryall; /* whether to try all extensions */
  102.    int extcount;
  103.    char *extp;
  104.    static char ext[3][5] = {".COM", ".EXE", ".BAT"};
  105.  
  106.    count = 0;
  107.  
  108.    /* is there an extension and is it in the last path component? */
  109.    tryall = !(extp = strrchr (fname, '.')) || strpbrk (extp+1, "\\/");
  110.  
  111.    /* if a path is already specified, then just try tacking on .com, .exe */
  112.    if (strstr (fname, "\\") || fname[1] == ':')
  113.    {
  114.       if (tryall == 0)
  115.       {
  116.      strcpy (fullname, fname);
  117.      return 1;
  118.       }
  119.  
  120.       extp = stpcpy (fullname, fname);
  121.  
  122.       for (extcount = 0; extcount < 3; extcount++)
  123.       {
  124.      strcat (extp, ext[extcount]);
  125.  
  126.      if (findfirst (fullname, &f, 7) == 0)
  127.        return 1;
  128.       }
  129.    }
  130.    else while(paths[count]) /* cycle through paths */
  131.    {
  132.       if(tryall)
  133.       {
  134.      /* create base filename w/o extensions */
  135.      /* stpcpy non-ANSI... might want to change this */
  136.      extp = stpcpy (stpcpy (stpcpy (fullname, paths[count]), "\\"), fname);
  137.  
  138.      for (extcount = 0; extcount < 3; extcount++)
  139.      {
  140.         strcpy (extp, ext[extcount]);
  141.  
  142.         if(findfirst(fullname, &f, 7) == 0)
  143.           return 1;
  144.      }
  145.       }
  146.       else
  147.       {
  148.      strcpy(fullname, paths[count]);
  149.      strcat(fullname, "\\");
  150.      strcat(fullname, fname);
  151.  
  152.      if(findfirst(fullname, &f, 7) == 0)
  153.        return 1;
  154.       }
  155.       count++;
  156.    }
  157.  
  158.    return 0;
  159. }
  160.